package weixin

import (
	"data/config"
	"public/mycache"
	"public/mysqldb"
	"time"
)

/*
	更新用户信息
*/
func UpdateInfo(info User_wx_info_tbl) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	isUpdate := true
	var _tmp User_wx_info_tbl
	orm.Where("open_id = ? and type = ?", info.Open_id, info.Type).Find(&_tmp)
	if _tmp.Id > 0 {
		info.Id = _tmp.Id
		if info == _tmp {
			isUpdate = false
		}
	}

	if isUpdate {
		num := orm.Save(&info).RowsAffected
		//更新缓存
		cache := mycache.OnGetCache("wx_user_cache")
		cache.Add(info.Open_id, info, 2*time.Hour) //2小时超时时间s
		//---------------end
		return num > 0
	}

	return true
}

/*
	获取用户基本信息
*/
func OnGetUserInfo(openid, typ string) (info User_wx_info_tbl) {
	//获取
	cache := mycache.OnGetCache("wx_user_cache")
	var tp interface{}
	tp, ret := cache.Value(openid)
	if ret {
		info = tp.(User_wx_info_tbl)
		return
	}

	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var wx_info User_wx_info_tbl
	orm.Where("open_id = ? and type = ? ", openid, typ).Find(&info)
	if wx_info.Id > 0 {
		cache.Add(openid, info, 2*time.Hour) //2小时超时时间s
	}

	return
}
